--[[

k_smallblocks is a Minetest mod that adds smaller blocks to minetest aswell as 
its own node placement prediction/system

Copyright (C) 2019 Kurtzmusch

This file is part of k_smallblocks

k_smallblocks is free software; you can redistribute it and/or modify it under 
the terms of the GNU Lesser General Public License as published by the Free 
Software Foundation; either version 2.1 of the License, or (at your option) any
later version.

k_smallblocks is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with k_smallblocks. If not, see <https://www.gnu.org/licenses/>.

--]]


local function register_recipes( node_name, shapeless )
	local recipe_type = "shaped"
	if( shapeless ) then recipe_type = "shapeless" end
	local smallblock_recipes = {
		[1] = {
			type = recipe_type,
			output = "k_smallblocks:1_"..node_name.." 8",
      recipe = {
            { "default:"..node_name, "", "" },
						{ "", "", "" },
						{ "", "", "" }
      }
		},
		[2] = {
			type = recipe_type,
			output = "k_smallblocks:1_"..node_name.." 2",
			recipe = {
				{ "k_smallblocks:3_"..node_name, "", "" },
				{ "", "", "" },
				{ "", "", "" }
			}
		},

		[3] = {
			type = recipe_type,
			output = "k_smallblocks:1_"..node_name.." 4",
			recipe = {
				{ "k_smallblocks:15_"..node_name, "", "" },
				{ "", "", "" },
				{ "", "", "" }
			}
		},
		[4] = {
			type = recipe_type,
			output = "k_smallblocks:1_"..node_name.." 6",
			recipe = {
				{ "k_smallblocks:63_"..node_name, "", "" },
				{ "", "", "" },
				{ "", "", "" }
			}
		},

	}
	local halfslab_recipe = {
		type = recipe_type,
		output = "k_smallblocks:3_"..node_name,
		recipe = {
			{ "k_smallblocks:1_"..node_name, "k_smallblocks:1_"..node_name,"" },
			{ "", "", "" },
			{ "", "", "" }
		}
	}	
	local slab_recipes = {
		[1] = {
			type = recipe_type,
			output = "k_smallblocks:15_"..node_name,
			recipe = {
				{ "k_smallblocks:1_"..node_name, "k_smallblocks:1_"..node_name,"" },
				{ "k_smallblocks:1_"..node_name, "k_smallblocks:1_"..node_name, "" },
				{ "", "", "" }
			}
		},
		[2] = {
			type = recipe_type,
			output = "k_smallblocks:15_"..node_name,
			recipe = {
				{ "k_smallblocks:3_"..node_name, "", "" },
				{	"k_smallblocks:3_"..node_name, "", "" },
				{	"", "", "" }
			}
		}
	}
	local fullNode_recipes = {
		--TODO
	}

	local stair_recipe = {
		type= recipe_type,
		output = "k_smallblocks:63_"..node_name,
		recipe = {
			{ "k_smallblocks:3_"..node_name, "", "" },
			{ "k_smallblocks:3_"..node_name, "k_smallblocks:3_"..node_name, "" },
			{ "", "", "" }
		}
	}
	for key_int, val_recipe in pairs( smallblock_recipes ) do
		minetest.register_craft( val_recipe )
	end
	minetest.register_craft( halfslab_recipe )
	for key_int, val_recipe in pairs( slab_recipes ) do
		minetest.register_craft( val_recipe )
	end
	minetest.register_craft( stair_recipe )
end

local function register_nodes( node_name )
	for key_int, val_bitmap_name in pairs( smallblocks.origin_bitmaps ) do
		local origin_bitmap_nodebox = smallblocks.nodeboxes[key_int]
		if( bitmap_name_definition_map[val_bitmap_name] ) then
			minetest.register_node("k_smallblocks:".. val_bitmap_name.."_"..node_name,
				{
					description = bitmap_name_definition_map[val_bitmap_name].description,
					paramtype = "light",
					paramtype2 = "facedir",
					drawtype = "nodebox",
					tiles = { {name="default_stone_brick.png", align_style="world"} },
					is_ground_content = false,
					groups = { cracky=2, not_in_creative_inventory= 0},
					sunlight_propagates = true,
					node_box = {
						type = "fixed",
						fixed = origin_bitmap_nodebox 
					},
					on_rightclick = common.on_right_click,
					on_punch = function()
						minetest.chat_send_all( "node got punched" )
					end,

					on_use = bitmap_name_definition_map[val_bitmap_name].on_use
				}
			)
		else
			minetest.register_node("k_smallblocks:"..val_bitmap_name.."_"..node_name,
			{
				description = node_name.." odd shape",
				paramtype = "light",
				paramtype2 = "facedir",
				drawtype = "nodebox",
				tiles = { {name="default_stone_brick.png", align_style="world"} },
				is_ground_content = false,
				groups = { cracky=2, not_in_creative_inventory= 1},
				sunlight_propagates = true,
				node_box = {
					type = "fixed",
					fixed = origin_bitmap_nodebox 
				},
				on_rightclick = common.on_right_click,

on_punch = function()
						minetest.chat_send_all( "node got punched" )
					end,

			}
		)

		end
	end
end


smallblocks.register_node = function( node_name, generate_recipes, shapeless )
	register_nodes( node_name )
	if( generate_recipes ) then register_recipes( node_name, shapeless ) end
end
